home *** CD-ROM | disk | FTP | other *** search
/ Netware Super Library / Netware Super Library.iso / pegamail / pmfxuucp / source / mailfixf.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-05  |  11.4 KB  |  323 lines

  1. // Mailfixf.cpp - Program to Process incoming information from FXUUCICO
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. void process1(void);
  8. void find_name(void);
  9.  
  10.  
  11. // out  is used to output the updated message files
  12. // out2 is used to output the batch file OUTPUT.BAT
  13. // to move the mail to the home mailboxes
  14. FILE *out, *out2;
  15. // a - Global Line used by main and process file buffers
  16. char a[2048], bufr[2048], bufr2[2048], bufr3[2048], b[2048], n[32];
  17. char athost[40],hostexclaim[40],hostexclaim2[40],postmaster[40];
  18.  
  19. // main requires the local host name
  20. void main(void)
  21. {
  22.     FILE  *in;        // File for getting filenames
  23.     int j;
  24.     char ch;
  25.     char HostName[60]="NOT-DEFINED";                 // LOCAL HOST NAME
  26.     char FullName[60]="NOT-DEFINED";                    // FULL HOST AND LOCAL
  27.  
  28.     // Program uses Host.Dat file for storing Information about Host and timezone
  29.     // Format is 3 lines:
  30.     // HOST:  local name   Example:  HOST:  GCCcc
  31.     // FULL:  full host              FULL:  GCCcc.Guam.Net
  32.     // TIME:  timezone               TIME:  GST +1400
  33.     // ORGAN: organization           ORGAN: Guam Community College Computer Center
  34.     // POST:  postmaster             POST:  MICHAEL
  35.     // The keywords must be in ALL caps and Left justified including the :
  36.     // The values can have spaces before and/or after them
  37.     // Any lines with other values are ignored.
  38.     // Information can be in any order.
  39.     // File goes in the Spool directory
  40.  
  41.     // Open File
  42.     if ((in = fopen("HOST.DAT","r")) == NULL)
  43.     {
  44.         fprintf(stderr,"Open of Host file Host.Dat failed\n");
  45.         exit(1);
  46.     }
  47.     // Read Host Information from File
  48.     while (!feof(in))
  49.     {
  50.         j=0;                        // Initialize Array Pointer to Beginning
  51.         while(((ch=fgetc(in))!='\n') && !feof(in))    // Read Characters till End of Line or End of File
  52.             a[j++]=ch;                // Store Character
  53.         a[j]=NULL;                    // Add NULL to end of String
  54.         if (strstr(a,"HOST:")!=0)                  // Check for HOST: Line
  55.             strcpy(HostName,&a[5]);                // If Yes, copy Information after HOST:
  56.         if (strstr(a,"FULL:")!=0)            // Check for FULL: Line
  57.             strcpy(FullName,&a[5]);                 // If Yes, copy Information after FULL:
  58.         if (strstr(a,"POST:")!=0)            // Check for POST: Line
  59.             strcpy(postmaster,&a[5]);               // If Yes, copy Information after POST:
  60.     }
  61.     fclose(in);                        // Close file
  62.  
  63.     // Trim Trailing blanks for fields
  64.     while (HostName[strlen(HostName)-1]==' ')            HostName[strlen(HostName)-1]=NULL;
  65.     while (FullName[strlen(FullName)-1]==' ')                       FullName[strlen(FullName)-1]=NULL;
  66.     while (postmaster[strlen(postmaster)-1]==' ')                   postmaster[strlen(postmaster)-1]=NULL;
  67.  
  68.     // Trim Leading blanks from fields
  69.     while (HostName[0]==' ')                    strcpy(HostName,&HostName[1]);
  70.     while (FullName[0]==' ')                                        strcpy(FullName,&FullName[1]);
  71.     while (postmaster[0]==' ')                                      strcpy(postmaster,&postmaster[1]);
  72.  
  73.     // Create Variables used to find Users Name
  74.     // HostName is used for format MICHAEL@GCCCC.GUAM.NET
  75.     strupr(HostName);                // GCCCC
  76.     strcpy(athost,"@");               // Create @host name
  77.     strcat(athost,HostName);    // @GCCCC.GUAM.NET
  78.     // Host exclaim is for format NET!GUAM!GCCCC!MICHAEL
  79.     strcpy(hostexclaim,HostName);   // Create !host name
  80.     strcat(hostexclaim,"!");        // GCCCC!
  81.  
  82.     strupr(FullName);               // GCCCC.GUAM.NET
  83.     // Host exclaim2 is for format GCCCC.GUAM.NET!MICHAEL or
  84.     //                      format GCCCC.GUAM.NET!MICHAEL!KUENTOS.GUAM.NET
  85.     strcpy(hostexclaim2,FullName);
  86.     strcat(hostexclaim2,"!");    // GCCCC.GUAM.NET!
  87.  
  88.     // Open Input file: File is a Directory of *.D files Redirected to File
  89.     if ((in = fopen("temp.$$$", "rt")) == NULL)
  90.     {
  91.         fprintf(stderr, "Cannot open input file.\n");
  92.         exit(1);
  93.     }
  94.     if (setvbuf(in, bufr2, _IOFBF, 2048) != 0)        // Open a Input File Buffer
  95.         printf("failed to set up buffer for input file\n");
  96.     // Open Output file to create batch file for moving Mail
  97.     if ((out2 = fopen("OUTPUT.bat", "wt")) == NULL)
  98.         fprintf(stderr, "Cannot open output file.\n");
  99.  
  100.     // Read until End of File
  101.     while (!feof(in))
  102.     {
  103.         j=0;                        // Initialize Array Pointer to Beginning
  104.         while(((ch=fgetc(in))!='\n') && !feof(in))    // Read Characters till End of Line or End of File
  105.         {
  106.             a[j++]=ch;                // Store Character
  107.         }
  108.         a[j]=NULL;                    // Add NULL to end of String
  109.         if (a[9]=='D')                    // Check if Line Read has D as file extension
  110.             process1();                // Process the File Name from Line
  111.     }
  112.     fclose(in);                         // Close Input File
  113.     fprintf(out2,"IF EXIST TEMP.$$$ DEL TEMP.$$$\n");
  114.     fprintf(out2,"FXCLEAN\n");                // Batch file for final Clean Up
  115.     fclose(out2);
  116. }
  117.  
  118. // Routine to Process the File to make corrections if necessary
  119. // Process will put Quotes around From Name if they were not originally
  120. // included. Without the Quotes, the Pegasus mail reply will not work properly.
  121. // From: John Doe <address>
  122. // From: "John Doe" <address>
  123. // The Program will also wrap lines that are longer than 80 character, and
  124. // occur after the To: line, so routing lines are not changed.
  125. void process1(void)
  126. {
  127.     FILE  *in1;
  128.     char f[15], g[15], x[15], ch;
  129.     int i, j, y;
  130.     i=0;                        // Initialize Variable
  131.     n[0]=NULL;                    // Set n string to NULL
  132.     while (a[i]!=' ')                // Copy Letters from line of Input file
  133.     {                        // to File name String
  134.         f[i]=a[i];
  135.         i++;
  136.     }
  137.     f[i]=NULL;                    // Add NULL to terminate string
  138.     strcpy(x,f);
  139.     strcat(x,".X");
  140.     strcat(f,".D");                    // Append File Extension
  141.     strcpy(g,f);                    // Copy F Filename String to G File Name String
  142.     strcat(g,"XX");                    // Add XX to G Filename extension
  143.     // f  is the name of the file to process
  144.     // g  is the processed file.
  145.     // f  is read. filename.z
  146.     // g  is the processed output of f. filename.z to filename.zxx
  147.  
  148.     if ((out = fopen(g, "wt")) == NULL)            // Open Output file
  149.     {                                                    // filename.zxx
  150.         fprintf(stderr, "Cannot open output file.\n");
  151.         exit(1);
  152.     }
  153.     if (setvbuf(out, bufr3, _IOLBF, 2048) != 0)
  154.         printf("failed to set up buffer for output file\n");
  155.  
  156.     if ((in1 = fopen(x, "rt")) == NULL)            // Open Input file
  157.     {                            // filename.z (Current Directory)
  158.         fprintf(stderr, "Cannot open input file.\n");
  159.         exit(1);
  160.     }
  161.     if (setvbuf(in1, bufr, _IOFBF, 2048) != 0)        // Open a Input File Buffer
  162.         printf("failed to set up buffer for input file\n");
  163.  
  164.     while (!feof(in1))            // Process Mail Message till End of File
  165.     {
  166.         j=0;                // Initialize Pointer
  167.         while(((ch=fgetc(in1))!='\n') && !feof(in1))    // read characters till End of Line
  168.         {                        // or End of File
  169.             a[j]=ch;                // Store Character
  170.             j++;                    // Increment Counter
  171.         }
  172.         a[j]=NULL;                    // Terminate String
  173.         if (strstr(a,"C rmail")!=0)
  174.         {
  175.             strcpy(b,&a[8]);
  176.             if (strstr(b,"@")==0 && strstr(b,"!")==0)
  177.             {
  178.                 strupr(b);
  179.                 strcpy(n,b);
  180.             }
  181.             else
  182.             {
  183.                 strupr(b);
  184.                 find_name();
  185.             }
  186.         }
  187.     }
  188.     fclose(in1);
  189.     if ((in1 = fopen(f, "rt")) == NULL)            // Open Input file
  190.     {                            // filename.z (Current Directory)
  191.         fprintf(stderr, "Cannot open input file.\n");
  192.         exit(1);
  193.     }
  194.     if (setvbuf(in1, bufr, _IOFBF, 2048) != 0)        // Open a Input File Buffer
  195.         printf("failed to set up buffer for input file\n");
  196.  
  197.     while (!feof(in1))            // Process Mail Message till End of File
  198.     {
  199.         j=0;                // Initialize Pointer
  200.         while(((ch=fgetc(in1))!='\n') && !feof(in1))    // read characters till End of Line
  201.         {                        // or End of File
  202.             a[j]=ch;                // Store Character
  203.             j++;                    // Increment Counter
  204.         }
  205.         a[j]=NULL;                    // Terminate String
  206.         strncpy(b,a,5);                    // Copy First 5 characters
  207.         // Check if Line is the From: Line
  208.         // Check if Line already has '"'
  209.         // Check if Line has a '<' in it
  210.         // I have only seen this problem in lines with <address>
  211.         if (strstr(b,"From:")!=0 && strstr(a,"<")!=NULL)
  212.         {
  213.             while(a[6]==' ')                 // Trim Blanks between From:
  214.                 strcpy(&a[6],&a[7]);
  215.             if (a[6]!='"' && a[6]!='<')        // Check if already have "
  216.             {
  217.                 strcpy(b,"From: ");             // Copy "From: " to b string
  218.                 b[6]='"';                // Insert "
  219.                 j=7;                               // Set pointer
  220.                 for(i=6;i<=strlen(a);i++)        // Copy rest of Line
  221.                 {
  222.                     if (a[i]=='<')            // Check for '<' of address
  223.                     {
  224.                         b[j-1]='"';          // Add Ending "
  225.                         b[j]=' ';               // Add Space
  226.                         j++;                    // Increment Pointer
  227.                     }
  228.                     b[j]=a[i];            // Copy character from a to b
  229.                     j++;                         // Increment Pointer
  230.                 }
  231.                 strcpy(a,b);                // Copy corrected b string to a
  232.             }
  233.         }
  234.         strcpy(b,a);                    // Copy a string to b string
  235.  
  236. // This Section finds the Name of the user the mail is address To:
  237. // It scans fo the FOR in the header lines or
  238. // For the TO: line if not found ealier
  239.         strupr(b);                             // Conver to Upper case
  240.         if ((strstr(b,"TO:")!=NULL ||            // Check for To: Line
  241.              strstr(b,"FOR")!=NULL) &&            // Check for FOR Line
  242.              n[0]==NULL)                                // Don't bother if Name already found
  243.                 find_name();
  244.  
  245.  
  246.         strcpy(b,a);                    // Reset b to a string (Not all caps)
  247.         if (!feof(in1) || strlen(b)>1)            // Print line to Screen and File
  248.         {                        // If not End of File
  249.             printf("%s\n",b);                        // or strlen(b)>1
  250.             fprintf(out,"%s\n",b);                  // This elimanates adding a blank line if
  251.         }                                            // End file marker is on a separate line
  252.     }
  253.     fclose(in1);                        // Close Input File
  254.     fclose(out);                        // Close Output File
  255.     strcpy(a," ");
  256.     if (n[0]==NULL)
  257.     {
  258.         strcpy(n,postmaster);                // Postmaster Name if Name not Found
  259.         strcpy(a,"No Name");
  260.     }
  261.     // Call Movemail Batch file with filename postmaster name
  262.     fprintf(out2,"CALL MOVEMAIL %s %s %s %s\n",g,postmaster,n,a);
  263. }
  264.  
  265. void find_name(void)
  266. {
  267.     char c[256], *p;
  268.     int y;
  269.     p=strstr(b,athost);            // Search for @ type Address
  270.     if (p!=NULL)                // If Found
  271.     {
  272.         strcpy(p,"\0");                   // Put NULL at @ position to truncate string
  273.         strcpy(c,b);            // Copy truncated string
  274.         y=strlen(c);
  275.         while (c[y]!=' ' &&        // Look from last position
  276.                c[y]!=':' &&             // Until we find one of these
  277.                c[y]!='<' &&        // Characters
  278.                y>=0)
  279.                 y--;        // Decrement pointer
  280.         y++;                // Move the Pointer up one
  281.         p=&c[y];            // Set Pointer to that character
  282.         strcpy(n,p);            // Copy from that point to end of string (NAME)
  283.     }
  284.     else
  285.     {
  286.         p=strstr(b,hostexclaim);        // If @ address not found, check for ! address
  287.         if (p!=NULL)            // If ! address found
  288.         {
  289.             strcpy(c,(p+strlen(hostexclaim)));         // Copy string skipping over host!
  290.             y=2;
  291.             while (c[y]!=',' &&    // Move pointer until
  292.                    c[y]!='>' &&    // finding one of these characters
  293.                    c[y]!=')' &&
  294.                    c[y]!=' ' &&    // or finding the end of string
  295.                    c[y]!='@' &&
  296.                    c[y]!='!' &&
  297.                    y<=strlen(c))
  298.                     y++;
  299.             c[y]=NULL;        // Terminate string at that point
  300.             strcpy(n,c);            // Copy String to NAME
  301.         }
  302.         else
  303.         {
  304.             p=strstr(b,hostexclaim2);        // If @ address not found, check for ! address
  305.             if (p!=NULL)            // If ! address found
  306.             {
  307.                 strcpy(c,(p+strlen(hostexclaim2)));         // Copy string skipping over host!
  308.                 y=2;
  309.                 while (c[y]!=',' &&    // Move pointer until
  310.                     c[y]!='>' &&    // finding one of these characters
  311.                     c[y]!=')' &&
  312.                     c[y]!=' ' &&    // or finding the end of string
  313.                     c[y]!='@' &&
  314.                     c[y]!='!' &&
  315.                     y<=strlen(c))
  316.                         y++;
  317.                 c[y]=NULL;        // Terminate string at that point
  318.                 strcpy(n,c);            // Copy String to NAME
  319.             }
  320.         }
  321.     }
  322. }
  323.